SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


CREATE PROCEDURE dbo.[DnnProduct_AddFile]
 @PortalId   int,
 @UniqueId   uniqueidentifier,
 @VersionGuid  uniqueidentifier,
 @FileName   nvarchar(246),
 @Extension   nvarchar(100),
 @Size    int,
 @Width    int,
 @Height    int,
 @ContentType  nvarchar(200),
 @Folder    nvarchar(246),
 @FolderID   int,
 @CreatedByUserID   int,
 @Hash     varchar(40),
 @LastModificationTime	DATETIME
AS
BEGIN
 DECLARE @FileID int

 UPDATE dbo.DnnProduct_Files
 SET
  /* retrieves FileId from table */
  @FileID = FileId,
  FileName = @FileName,
  VersionGuid = @VersionGuid,
  Extension = @Extension,
  Size = @Size,
  Width = @Width,
  Height = @Height,
  ContentType = @ContentType,
  Folder = @Folder,
  FolderID = @FolderID,
  LastModifiedByUserID = @CreatedByUserID,
  LastModifiedOnDate = getdate(),
  SHA1Hash = @Hash,
  LastModificationTime = @LastModificationTime
 WHERE
  FolderID = @FolderID AND FileName = @FileName

 IF @@ROWCOUNT = 0
 BEGIN
    INSERT INTO dbo.DnnProduct_Files (
   PortalId,
   UniqueId,
   VersionGuid,
   FileName,
   Extension,
   Size,
   Width,
   Height,
   ContentType,
   Folder,
   FolderID,
   CreatedByUserID,
   CreatedOnDate,
   LastModifiedByUserID,
   LastModifiedOnDate,
   SHA1Hash,
   LastModificationTime
    )
    VALUES (
   @PortalId,
   @UniqueId,
   @VersionGuid,
   @FileName,
   @Extension,
   @Size,
   @Width,
   @Height,
   @ContentType,
   @Folder,
   @FolderID,
   @CreatedByUserID,
   getdate(),
   @CreatedByUserID,
   getdate(),
   @Hash,
   @LastModificationTime
    )

  SELECT @FileID = SCOPE_IDENTITY()
 END

 SELECT @FileID
END
GO
